package cn.tedu.wx.controller;

import cn.tedu.wx.constant.WxUrlConstant;
import cn.tedu.wx.service.IWxService;
import cn.tedu.wx.util.HttpClientUtil;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/wx")
public class WxController {

    @Autowired
    private IWxService wxService;

    @RequestMapping("/createMenu")
    public void CreateMenu(){
        // 准备路径
        String url = WxUrlConstant.CREATE_MENU.replace("ACCESS_TOKEN",getAccessToken());
        // 准备数据
        WxMenu wxMenu = new WxMenu();
        // 准备一级菜单
        List<WxMenuButton> buttons = new ArrayList<>();

        // 按钮1
        WxMenuButton wxMenuButton = new WxMenuButton();
        wxMenuButton.setName("跳转链接");
        List<WxMenuButton> wxMenuButtons=new ArrayList<>();
        wxMenuButton.setSubButtons(wxMenuButtons);

        WxMenuButton wxMenuButtonone = new WxMenuButton();
        wxMenuButtonone.setName("你🐎的给我百度去");
        wxMenuButtonone.setType(WxConsts.MenuButtonType.VIEW);
        wxMenuButtonone.setUrl("https://www.baidu.com/");
        wxMenuButtons.add(wxMenuButtonone);

        WxMenuButton wxMenuButtontwo = new WxMenuButton();
        wxMenuButtontwo.setName("你🐎的给我哔哩哔哩去");
        wxMenuButtontwo.setType(WxConsts.MenuButtonType.VIEW);
        wxMenuButtontwo.setUrl("https://www.bilibili.com/");
        wxMenuButtons.add(wxMenuButtontwo);

        WxMenuButton wxMenuButtontree = new WxMenuButton();
        wxMenuButtontree.setName("你🐎的给我优酷去");
        wxMenuButtontree.setType(WxConsts.MenuButtonType.VIEW);
        wxMenuButtontree.setUrl("https://www.youku.com/");
        wxMenuButtons.add(wxMenuButtontree);


        // 按钮2
        WxMenuButton wxMenuButton1 = new WxMenuButton();
        wxMenuButton1.setName("点我扫码");
        wxMenuButton1.setType(WxConsts.MenuButtonType.SCANCODE_WAITMSG);
        wxMenuButton1.setKey("rselfmenu_0_0");
        buttons.add(wxMenuButton1);

        // 按钮3
        WxMenuButton wxMenuButton2 = new WxMenuButton();
        wxMenuButton2.setName("点我扫🐎");
        wxMenuButton2.setType(WxConsts.MenuButtonType.SCANCODE_PUSH);
        wxMenuButton2.setKey("rselfmenu_0_1");
        buttons.add(wxMenuButton2);

        buttons.add(wxMenuButton);
        wxMenu.setButtons(buttons);
        String json = wxMenu.toJson();
        String s = HttpClientUtil.doPostJson(url, json);
        System.out.println(s);
    }


    /**
     *  获取Access token
     */
    @RequestMapping("/getAccessToken")
    public String getAccessToken(){
        return wxService.getAccessToken();
    }
    /**
     *
     * @param signature 微信加密签名，signature结合了开发者填写的 token 参数和请求中的 timestamp 参数、nonce参数。
     * @param timestamp 时间戳
     * @param nonce 随机数
     * @param echostr 随机字符串
     * @return echostr
     */
    @GetMapping
    public String checkWx(String signature,String timestamp,String nonce,String echostr){
        try{
            wxService.checkWx(signature, timestamp, nonce, echostr);
        }catch (RuntimeException e) {
            return "";
        }
        return echostr;
    }

    // 接收用户发的消息
    @PostMapping
    public String sendMsg(@RequestBody String message){
        System.out.println(message);
        return message;
    }
}
